package com.eportal.action.background;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.eportal.action.BaseAction;
import com.eportal.domain.Sysopt;
import com.eportal.domain.TAgent;
import com.eportal.domain.TMirrorAgent;
import com.eportal.exception.MdKnownRunTimeException;
import com.eportal.service.SysmenuService;
import com.eportal.service.TAgentService;
import com.eportal.service.TMirrorAgentService;
import com.eportal.util.CityUtil;
import com.eportal.util.DateUtils;
import com.eportal.util.HomePageConfig;
import com.framework.jqgrid.JqGridPager;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 公众号、小程序管理
 * @author wangk
 *
 */
public class TMirrorAgentAction extends BaseAction implements ModelDriven<TMirrorAgent> {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Logger log = Logger.getLogger(TMirrorAgentAction.class.getName());

    private Map<String, Object> result = new HashMap<String, Object>();
    
    @Autowired
    private TMirrorAgentService tMirrorAgentService;
	/**
     */
    public String listView() {
        return SUCCESS;
    }
    


    /**
     * 
     */
    public void list() throws Exception {
		try {

			JqGridPager page = new JqGridPager();
			Sysopt user = (Sysopt) getSession().getAttribute("user");
			int userRole = (Integer) getSession().getAttribute("userRole");
			String cname = getRequest().getParameter("cname");
			StringBuilder where = new StringBuilder(" gtype='3'");
			if (userRole == 1 || userRole == 2) {
				if (cname != null && cname.length() > 0) {
					where.append(" and  cname like '%" + cname + "%'");
				}

			}

			page.setWhere(where.toString());
			setPage(getRequest(), page);
			List<Map> list = tMirrorAgentService.list(page);
			List datalist = new ArrayList();
			for (Map bean : list) {
				Map m = new HashMap();
				m.put("SID", bean.get("SID"));
				m.put("CNAME",bean.get("CNAME"));
				m.put("GID", bean.get("GID"));
				m.put("WX_APP_ID", bean.get("WX_APP_ID"));
				m.put("CTIME_", DateUtils.formatDate((Date)bean.get("CTIME")));
				datalist.add(m);
			}

			jqGridQueryMap(Map.class, getRequest(), datalist, getResponse(), page);
		}catch(Exception e){
    		log.error(this, e);
    	}
    	
    }

    public String addView() {
    	
        return SUCCESS;
    }
    
    public void add() {
    	try{
    		TMirrorAgent bean = getModel();
        	bean.setmType("1");
        	bean.setgType("3");
        	bean.setcTime(new Timestamp(System.currentTimeMillis()));
        	bean.setAgtCode("1001");
        	tMirrorAgentService.save(bean);
        	result.put("code", "1");
			result.put("msg", "保存成功");
    	}catch(Exception e){
    		result.put("code", "-1");
			result.put("msg", "保存失败");
    		log.error(this,e);
    	}
        responseBody(result);
    }
    
    public String editView() {
    	String sid = getRequest().getParameter("sid");
    	TMirrorAgent agent = tMirrorAgentService.get(Long.valueOf(sid));
    	getRequest().setAttribute("tmirrorAgent", agent);
    	
        return SUCCESS;
    }
    
    public void edit() {
    	try{
    		TMirrorAgent bean = getModel();
    		TMirrorAgent agent = tMirrorAgentService.get(bean.getSid());
    		agent.setWxAppId(agent.getWxAppId());
    		agent.setcName(bean.getcName());
    		agent.setWxAppSecret(bean.getWxAppSecret());
        	tMirrorAgentService.update(agent);
        	result.put("code", "1");
			result.put("msg", "保存成功");
    	}catch(Exception e){
    		result.put("code", "-1");
			result.put("msg", "保存失败");
    		log.error(this,e);
    	}
        responseBody(result);
    }
    
    
    private TMirrorAgent tMirrorAgent = new TMirrorAgent();

    @Override
    public TMirrorAgent getModel() {
        return tMirrorAgent;
    }
}
